package com.infore.metadata.core.service.queryImpl;

import com.infore.metadata.core.interfaces.MetadataQueryInterfaces;
import com.infore.metadata.core.model.MetadataConfig;
import com.infore.metadata.core.model.SimpleDataSource;
import com.infore.metadata.core.utils.JdbcTool;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 默认的查询实现，MySQL 等默认使用该方式
 *
 * @author ：lijh
 * @date 创建时间： 2022/12/19 11:11
 */
public class DefaultQueryService implements MetadataQueryInterfaces {

    /**
     * 获取表名和注释映射
     * @param simpleDataSource 数据源配置
     * @param metadataConfig 配置信息
     */
    @Override
    public Map<String, String> getTable(SimpleDataSource simpleDataSource, MetadataConfig metadataConfig) {
        Map<String, String> answer = new HashMap<String, String>();

        try {
            Connection connection = simpleDataSource.getConnection();
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet rs = metaData.getTables(metadataConfig.getCatalog(), metadataConfig.getSchemaPattern(), metadataConfig.getTableNamePattern(), null);

            while (rs.next()) {
                answer.put(rs.getString("TABLE_NAME"), rs.getString("REMARKS"));
            }

            JdbcTool.closeResultSet(rs);

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return answer;
    }

    /**
     *  获取表名和列名-注释映射
     * @param simpleDataSource 数据源配置
     * @param metadataConfig 配置信息
     */
    @Override
    public Map<String, Map<String, String>> getColumn(SimpleDataSource simpleDataSource, MetadataConfig metadataConfig) {
        return null;
    }


}
